package a66weding.com.jiehuntong.pager;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.ToolsWedBudgetActivity;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.beans.BudgetBean;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.GiftBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.common.CommonAdapter;
import a66weding.com.jiehuntong.common.MyViewHolder;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/6/2
 * @description :
 */
public class ToolsPager_Budget_RecommendListEdit extends BasePager implements View.OnClickListener, OnNetResultListener {
    private ToolsWedBudgetActivity mBudgetActivity;
    private View mContentview;
    private TextView mCityTv;
    private ImageView mDownArrIv;
    private TextView mTitleTv;
    private TextView mMoneyCountTv;
    private ListView mBudgetLview;
    private LinearLayout mBackLay;
    private ListViewAdapter mAdapter;

    private List<BudgetBean.DataBean> budgetList;
    private int budgetCount;
    private boolean isEdit;

    private int mCurrentSelection;
    private BudgetBean budgetBean;

    public ToolsPager_Budget_RecommendListEdit(Activity activity) {
        super(activity);
    }


    @Override
    protected View getView() {
        mBudgetActivity= (ToolsWedBudgetActivity) mActivity;

        mContentview = View.inflate(mActivity, R.layout.pager_tools_budget_editrecommend,null);
        mCityTv = (TextView) mContentview.findViewById(R.id.tv_tools_wedregistry_place);
        mDownArrIv = (ImageView) mContentview.findViewById(R.id.iv_head_downarr);
        mTitleTv = (TextView) mContentview.findViewById(R.id.tv_tools_dayplan_title);
        mBackLay = (LinearLayout) mContentview.findViewById(R.id.llay_tools_wedregister_back);
        mMoneyCountTv = (TextView) mContentview.findViewById(R.id.tv_tools_budgetmoney);
        mBudgetLview = (ListView) mContentview.findViewById(R.id.lview_tools_budget);

        mCityTv.setText("保存");
        mTitleTv.setText("修改结婚预算");
        mDownArrIv.setVisibility(View.GONE);

        mCityTv.setOnClickListener(this);
        mBackLay.setOnClickListener(this);

        return mContentview;
    }


    @Override
    public void initData() {
        User user = DataWareHourse.getInstance().getmUser();

        budgetBean = mBudgetActivity.getmBudgitData();
        budgetCount= mBudgetActivity.getBudgetCount();

        mMoneyCountTv.setText(""+budgetCount);


        if(budgetBean !=null) {

            budgetList = budgetBean.getData();
            if(budgetList!=null && budgetList.size()>0) {

                mAdapter=new ListViewAdapter();

                mBudgetLview.setAdapter(mAdapter);

            }
        }

    }

    class ListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return budgetList.size();
        }

        @Override
        public Object getItem(int position) {
            return budgetList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final BudgetBean.DataBean dataBean = budgetList.get(position);

            ViewHolder myHolder=null;

                myHolder=new ViewHolder();
                convertView=View.inflate(mBudgetActivity,R.layout.item_tools_budget_editcontentitem,null);

                myHolder.mNameTv= (TextView) convertView.findViewById(R.id.name);
                myHolder.mMoneyEt= (EditText) convertView.findViewById(R.id.et_tools_budgetmoney);

                myHolder.mNameTv.setText(dataBean.getWBItemName());
                myHolder.mMoneyEt.setHint(""+dataBean.getWBItemAmount());

            myHolder.mMoneyEt.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                            }

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {

                            }

                            @Override
                            public void afterTextChanged(Editable s) {
                                String money=s.toString().trim();
                                if(TextUtils.isEmpty(money)) {
                                    dataBean.setWBItemAmount(0);
                                }else{
                                    dataBean.setWBItemAmount(Integer.valueOf(money));

                                    int moneyAccount=0;
                                    for (int i = 0; i < budgetList.size(); i++) {
                                        moneyAccount += budgetList.get(i).getWBItemAmount();
                                    }
                                    mMoneyCountTv.setText(""+moneyAccount);
                                }
                            }
                        });

            myHolder.mMoneyEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                            @Override
                            public void onFocusChange(View v, boolean hasFocus) {
                                mCurrentSelection=position;
                            }
                        });

                if(mCurrentSelection==position) {
                    myHolder.mMoneyEt.requestFocus();
                }

            return convertView;
        }

        class ViewHolder {
            EditText mMoneyEt;
            TextView mNameTv;

        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

                case R.id.llay_tools_wedregister_back://返回
                    mBudgetActivity.getSupportFragmentManager().popBackStack();
                    break;
                case R.id.tv_tools_wedregistry_place://保存
                    int moneyAccount=0;
                    for (int i = 0; i < budgetList.size(); i++) {
                        moneyAccount += budgetList.get(i).getWBItemAmount();
                    }
                    mBudgetActivity.setBudgetCount(moneyAccount);
                    mBudgetActivity.setmBudgitData(budgetBean);
                    mBudgetActivity.getSupportFragmentManager().popBackStack();
                    mBudgetActivity.notifyPagerData("结婚预算明细页");

                    break;

        }
    }



    @Override
    public void onSucess(SuccessBean sucessbean)  {
        Object data = sucessbean.getData();
        if(data instanceof GiftBean) {
            GiftBean mGiftBean= (GiftBean) data;
            switch (mGiftBean.getResCode()) {
                    case "0000":
                    break;
                     case "0001":

                     case "0002":

                     case "0003":
                    break;
            }


        }else if(data instanceof CommonResultBean){
            CommonResultBean commonResult= (CommonResultBean) data;
            switch (commonResult.getResCode()) {
                    case "0000":
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
    }

}
